package com.health.yb.web.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.health.yb.web.bean.common.NameReq;
import com.health.yb.web.bean.common.Response;
import com.health.yb.web.bean.entity.YbUser;
import com.health.yb.web.bean.entity.YbUserInfo;
import com.health.yb.web.bean.req.UserMaterialReq;
import com.health.yb.web.bean.req.YbUserAddReq;
import com.health.yb.web.service.UserInfoService;
import com.health.yb.web.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * @author rjwang
 * @version 1.0
 * @date 2019/12/15 16:08
 */
@Api(tags = {"用户领取物品"})
@RestController
@Validated
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserInfoService userInfoService;

    @PostMapping("/user")
    @ApiOperation("新增用户,批量添加，以英文分号分开")
    public Response addUser(@Validated @RequestBody NameReq nameReq) throws Exception {
        String username = nameReq.getName();
        if (StrUtil.isBlank(username)) {
            return new Response().failureWithMsg("用户名称必须传");
        }
        return userInfoService.addUser(username);
    }
    @GetMapping("/user")
    @ApiOperation("查询所有用户")
    public Response<YbUserInfo> getUsers() {
        List<YbUserInfo> list = userInfoService.list(new LambdaQueryWrapper<YbUserInfo>());
        return new Response().successWithData(list);
    }

    @PostMapping("/user/material")
    @ApiOperation("新增用户领取物品")
    @ApiImplicitParam(name = "ybUserAddReq", value = "领取物品对象", dataType = "YbUserAddReq", paramType = "body", required = true)
    public Response addUserType(@Validated @RequestBody YbUserAddReq ybUserAddReq) throws Exception {
        return userService.addUserType(ybUserAddReq);
    }


    @GetMapping("/export/material")
    @ApiOperation("导出用户对用物资数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startDate", value = "领取物资开始时间", dataType = "string", paramType = "query", required = false),
            @ApiImplicitParam(name = "endDate", value = "领取物资结束时间", dataType = "string", paramType = "query", required = false)
    })

    public void exportStatiesData(@DateTimeFormat(pattern = "yyyy-MM-dd")String startDate,
                                      @DateTimeFormat(pattern = "yyyy-MM-dd") String endDate,
                                      HttpServletRequest request,
                                      HttpServletResponse httpServletResponse) throws Exception {
        userService.exportStatiesData(startDate,endDate,request,httpServletResponse);
    }

    @GetMapping("/user/material")
    @ApiOperation("查询物资数据")
    public Response<YbUser> getUserMaterial(@Validated UserMaterialReq userMaterialReq) throws Exception {
        return userService.getUserMaterial(userMaterialReq);
    }


}
